﻿using Microsoft.VisualBasic.Devices;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;


namespace WindowsFormsApp1
{
    public partial class 文件操作窗体 : Form
    {
        public string path;
        public 文件操作窗体()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //  Computer computer = new Computer();

            //1：修改文件夹名称
            // computer.FileSystem.RenameDirectory(@"E:\3", "4");

            //2：修改文件名称
            // computer.FileSystem.RenameFile(@"E:\1\1.TXT", "2.TXT");

            //   computer.FileSystem.RenameFile();

            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "选择文件夹"; ;//左上角提示
            string path = string.Empty;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                path = dialog.SelectedPath;//获取选中文件路径
            }

            DirectoryInfo di = new DirectoryInfo(path);

            FileInfo[] fi = di.GetFiles();

            Computer computer = new Computer();




            try
            {
                foreach (FileInfo fiTemp in fi)
                {
                    //MessageBox.Show(fiTemp.Name);
                    string newname = fiTemp.Name.Replace(textBox2.Text, textBox3.Text);
                    computer.FileSystem.RenameFile(path + "\\" + fiTemp.Name, newname);
                }
                MessageBox.Show("操作完成");
            }
            catch
            {
                MessageBox.Show("没有符合条件的文件");
            }




        }

        private void button2_Click(object sender, EventArgs e)
        {

            //1:当前文件夹
            //DirectoryInfo di = new DirectoryInfo(Environment.CurrentDirectory);
            // Create an array representing the files in the current directory.

            //2：指定文件夹
           // DirectoryInfo di = new DirectoryInfo(@"E:\1");

            //3：选择文件夹
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "选择匹配目录"; ;//左上角提示
            string path = string.Empty;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                path = dialog.SelectedPath;//获取选中文件路径
            }

            DirectoryInfo di = new DirectoryInfo(path);//di是文件夹里文件名的集合

            FileInfo[] fi = di.GetFiles();

            Computer computer = new Computer();

            
                try
                {
                foreach (FileInfo fiTemp in fi)
                {
                    //MessageBox.Show(fiTemp.Name);
                    string newname = fiTemp.Name.Replace(textBox1.Text, "");
                    computer.FileSystem.RenameFile(path + "\\"+fiTemp.Name, newname);
                }
                MessageBox.Show("操作完成");
            }
                catch
                {
                MessageBox.Show("没有符合条件的文件");
            }

            }

        private void button3_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "选择文件夹"; ;//左上角提示
            string path = string.Empty;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                path = dialog.SelectedPath;//获取选中文件路径
            }

            DirectoryInfo di = new DirectoryInfo(path);

            FileInfo[] fi = di.GetFiles();

            Computer computer = new Computer();


            try
            {
                foreach (FileInfo fiTemp in fi)
                {
                    //  string lastNChars = fiTemp.Name.Substring(fiTemp.Name.Length - 10);

                    string lastNChars = fiTemp.Name.Substring(fiTemp.Name.Length - 6,2);
                    //MessageBox.Show(fiTemp.Name);
                    string newname = fiTemp.Name.Replace(lastNChars, "");
                    computer.FileSystem.RenameFile(path + "\\" + fiTemp.Name, newname);
                }
                MessageBox.Show("操作完成");
            }
            catch
            {
                MessageBox.Show("没有符合条件的文件");
            }

           

        }

        private void 选择文件夹_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "选择文件夹"; ;//左上角提示
            string path = string.Empty;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                path = dialog.SelectedPath;//获取选中文件路径
            }
            label4.Text = path;
            DirectoryInfo di = new DirectoryInfo(path);

            FileInfo[] fi = di.GetFiles();
           
            DataTable dt = new DataTable();
            dt.Columns.Add("序号");
            dt.Columns.Add("旧名称");
            dt.Columns.Add("新名称");
            
            for (int i = 0; i < fi.Length-1; i++)
            {
                dt.Rows.Add(i+1, fi[i]);
            }
            dataGridView1.DataSource = dt;

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }




        //string lastNChars = str.Substring(str.Length - n);


    }
}

/*
 参考资料：
https://www.ecdove.com/p/28.html
https://blog.csdn.net/lihongmao5911/article/details/128844034
https://blog.csdn.net/naer_chongya/article/details/130606681
 
https://learn.microsoft.com/zh-cn/previous-versions/aa336804(v=msdn.10)

https://learn.microsoft.com/zh-cn/dotnet/api/system.io.fileinfo.name?view=net-7.0#system-io-fileinfo-name
 
 //指定目录及文件
 https://blog.csdn.net/qq_29728817/article/details/128146314
 
 
 
 
 */



//10月13日 
/*
 
1、 radiobutton控件
 几个radiobutton控件互斥，可以作为单选框
radioButton1.Checked 的值判断是否选中
         //private void button5_Click(object sender, EventArgs e)
        //{
        //    if (radioButton1.Checked)
        //    {
        //        MessageBox.Show("选中1");
        //    }
            
        //    if (radioButton2.Checked)
        //    {
        //        MessageBox.Show("选中2");
        //    }
       
        //}
 
 
2.字符串操作
    string NewChars = chars.Substring(6,2);//去掉从前往后数第6个开始，连续2个字符
    string NewChars = chars.Substring(fiTemp.Name.Length - 6,2);//去掉倒数第6个开始，连续2个字符
    string lastNewChars = chars.Substring(fiTemp.Name.Length - 6);//去掉最后6个字符
 
 
 */
/*
 绝对路径：是从盘符开始的路径，形如
C:\windows\system32\cmd.exe

相对路径：是从当前路径开始的路径，假如当前路径为C:\windows
要描述上述路径，只需输入
system32\cmd.exe
 
 在C#中，斜杠“\”是转义字符，表示路径是采用“\\”代替“\”，避免把这个字符解释为转义字符。如
“c:\\microfoft\\readme.txt”，也可以用“@”代替，在路径前加“@”符号，“\”就不会再被解释为转义符。如：@"c:\microfoft\readme.txt”
 
 1.Path是全称，包含文件夹路径、文件名和文件后缀名
2.Directory不是全称，只包含文件夹路径，不能包含文件名和文件后缀名
 */